package com.jianyun.jyzs.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import com.jianyun.jyzs.R;
import com.jianyun.jyzs.activity.EntNatureActivity;
import com.jianyun.jyzs.bean.EnNatureResponse;
import com.jianyun.jyzs.constant.SysConstant;

import java.util.List;

/**
 * Created by liml on 17/8/8.
 */

public class EnNatureAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<EnNatureResponse.EnNatureBean> list;

    public EnNatureAdapter(Context context, List<EnNatureResponse.EnNatureBean> list) {
        this.context = context;
        this.list = list;
    }

    public void setList(List<EnNatureResponse.EnNatureBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_project, null);
        return new MyViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        MyViewHolder myViewHolder = (MyViewHolder) holder;
        try {
            final EnNatureResponse.EnNatureBean bean = list.get(position);
            myViewHolder.textview.setText(bean.getName());
            myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.putExtra(SysConstant.msg_key_string1, bean.getId());
                    intent.putExtra(SysConstant.msg_key_string2, bean.getName());
                    ((EntNatureActivity) context).setResult(Activity.RESULT_OK, intent);
                    ((EntNatureActivity) context).finish();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        private final TextView textview;

        public MyViewHolder(View itemView) {
            super(itemView);
            textview = (TextView) itemView.findViewById(R.id.title);
        }
    }
}
